﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.Framework.Client.Utils;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.Framework.Common.Enums;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class PayTypeView : BaseUserControl
    {
        private DataSet dsPayType;
        public PayTypeView()
        {
            InitializeComponent();

            ButtonItemIconHelper.InitRibbonBar(this.rbMenu);
            this.InitPayType();
        }

        public PayTypeView(ClientAppBase app)
            : base(app)
        {
            this.InitializeComponent();

            ButtonItemIconHelper.InitRibbonBar(this.rbMenu);

            this.InitPayType();

        }

        private void InitPayType()
        {
            string error = string.Empty;
            dsPayType = new DataSet();
            string sFilter = "";
            if (BussinessProxyManager.Instance == null)
            {
                return;
            }
            bool bResult = BussinessProxyManager.Instance.QueryPayType(sFilter, out dsPayType, out error);
            if (!bResult)
            {
                MsgBox.MsgInfo("获取充值方式失败\r\n" + error);
                return;
            }
            gdPayType.DataSource = null;
            gdPayType.DataSource = dsPayType.Tables[0].DefaultView;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmAddPayType form = new frmAddPayType();
            form.StartPosition = FormStartPosition.CenterScreen;
            form.Flag = ControlClassModeEnum.emInsert;
            if (form.ShowDialog() == DialogResult.OK)
            {
                this.InitPayType();
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int iFocusedRowHandle = gvPayType.FocusedRowHandle;
            if (iFocusedRowHandle < 0)
            {
                MsgBox.MsgInfo("请先选择要修改的记录。");
                return;
            }

            DataRow dr = gvPayType.GetDataRow(iFocusedRowHandle);
            DBEntityPayType entity = new DBEntityPayType(dr);
            frmAddPayType form = new frmAddPayType(entity);
            form.StartPosition = FormStartPosition.CenterScreen;
            form.Flag = ControlClassModeEnum.emEdit;
            if (form.ShowDialog() == DialogResult.OK)
            {
                this.InitPayType();
            }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int iFocusedRowHandle = gvPayType.FocusedRowHandle;
            if (iFocusedRowHandle < 0)
            {
                MsgBox.MsgInfo("请先选择要删除的记录。");
                return;
            }

            DataRow dr = gvPayType.GetDataRow(iFocusedRowHandle);
            bool bResult = MsgBox.MsgQue(string.Format("确定要删除充值方式 '{0}' 吗？", dr[DBEntityPayType.FieldName.PNAME].ToString()));
            if (bResult)
            {
                string error = string.Empty;
                int iPayTypeID = Convert.ToInt32(dr[DBEntityPayType.FieldName.PAYTYPEID]);
                bResult = BussinessProxyManager.Instance.DeletePayType(iPayTypeID, out error);
                if (bResult == false)
                {
                    MsgBox.MsgInfo(error);
                    return;
                }
                else
                {
                    MsgBox.MsgInfo("删除成功。");
                    this.InitPayType();
                }
            }
        }

        private void gvPayType_DoubleClick(object sender, EventArgs e)
        {
            btnModify_Click(null, null);
        }
    }
}
